package com.nineclock.system.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.model.dto.*;
import com.nineclock.model.pojo.SysRole;
import com.nineclock.system.SysCompanyControllerApi;
import com.nineclock.system.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/company")
public class SysCompanyController implements SysCompanyControllerApi {

    @Autowired
    SysCompanyService sysCompanyService;
    @Override
    @GetMapping("/list/{userId}")
    public Result<List<SysCompanyDTO>> queryCompanyListByUserId(@PathVariable("userId") Long userId) {
        List<SysCompanyDTO> sysCompanyDTOList = sysCompanyService.queryCompanyListByUserId(userId);
        return Result.success(sysCompanyDTOList);
    }

    @Override
    @GetMapping
    public Result<SysCompanyDTO> queryCompanyInfo() {
        SysCompanyDTO sysCompanyDTO = sysCompanyService.queryCompanyInfo();
        return Result.success(sysCompanyDTO);
    }

    @Override
    @PostMapping("/uploadOSS")
    public Result<String> uploadCompanyLogo(@RequestBody MultipartFile file) {
        String logoUrl = sysCompanyService.uploadCompanyLogo(file);
        return Result.success(logoUrl);
    }

    @Override
    @PutMapping
    public Result updateCompanyInfo(@RequestBody SysCompanyDTO sysCompanyDTO) {

        sysCompanyService.updateCompanyInfo(sysCompanyDTO);
        return Result.success();
    }

    /**
     * 企业当前管理员
     */
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> getCurrentAdmin(){
        SysCompanyUserDTO sysCompanyUserDTO = sysCompanyService.getCurrentAdmin();
        return Result.success(sysCompanyUserDTO);
    }

    /**
     * 更换企业主管理员
     * @param sysChangeMainManagerDTO
     * @return
     */
    @PostMapping("/com.nineclock.approve.config/admin")
    public Result updateCompanyAdmin (@RequestBody SysChangeMainManagerDTO sysChangeMainManagerDTO) {

        sysCompanyService.updateCompanyAdmin(sysChangeMainManagerDTO);
        return Result.success();
    }

    /**
     * 查询企业角色
     * @param page, pageSize
     * @return
     */
    @GetMapping("/role")
    public PageResult pageRole(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        PageResult pageResult = sysCompanyService.pageRole(page,pageSize);
        return pageResult;
    }

    //根据企业的关键字名称, 模糊查询企业列表
    @Override
    @GetMapping("/list")
    public Result<List<SysCompanyDTO>> queryCompanyByName(String keyword) {
        List<SysCompanyDTO> companyDTOList = sysCompanyService.queryCompanyByName(keyword);
        return Result.success(companyDTOList);
    }

    /**
     * 申请加入企业
     * @param sysApplyJoinCompanyUserDTO
     * @return
     */
    @Override
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO sysApplyJoinCompanyUserDTO) {
        sysCompanyService.applyJoinCompany(sysApplyJoinCompanyUserDTO);
        return Result.success();
    }

    /**
     * 角色对应权限
     * 应该获取角色信息
     * @return
     */
    @GetMapping("/com.nineclock.approve.config/permissions")
    public Result<List<SysRoleDTO>> permissions(){
        List<SysRoleDTO> sysRoleDTOs = sysCompanyService.permissions();
        return Result.success(sysRoleDTOs);
    }
}
